package com.yanqu.road.dao.impl.activity.stelesoul;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.stelesoul.config.SteleSoulMapConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SteleSoulStoneSoulMapConfigDaoImpl extends TempDao {

    private static String getSteleSoulStoneSoulMapConfigSql = "select * from `t_s_activity_stonesoul_map` where `activityId`=?";

    public Map<Integer, Map<Integer, SteleSoulMapConfig>> getSteleSoulMapConfig(int activityId) {
        Map<Integer, Map<Integer, SteleSoulMapConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSteleSoulStoneSoulMapConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SteleSoulMapConfig steleSoulMapConfig = new SteleSoulMapConfig();
                steleSoulMapConfig.setActivityId(rs.getInt("activityId"));
                int period = rs.getInt("period");
                steleSoulMapConfig.setPeriod(period);
                steleSoulMapConfig.setCircle(rs.getInt("circle"));
                steleSoulMapConfig.setSurprisePlaid(rs.getInt("surprisePlaid"));
                steleSoulMapConfig.setReward(rs.getString("reward"));
                steleSoulMapConfig.setSurpriseReward(rs.getString("surpriseReward"));
                steleSoulMapConfig.setParamOne(rs.getString("paramOne"));
                steleSoulMapConfig.setParamTwo(rs.getString("paramTwo"));
                Map<Integer, SteleSoulMapConfig> insideMap = new ConcurrentHashMap<>();
                if(map.containsKey(period)){
                    insideMap = map.get(period);
                }
                insideMap.put(steleSoulMapConfig.getCircle(), steleSoulMapConfig);
                map.put(period, insideMap);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
